clear all	 
set maxvar 10000
	 
cd "~/Dropbox/UK-mass polarization"
use "BES2019_W23_Panel_v23.0.dta", clear 

/*
partyIdW* // w1-w4 w6-w21
redistSelfW* // w1-w4 w6-w7 w10-w21
redistLabW* // w1-w4 w6-w7 w10-w21
leftRightW // w1-w21
EUIntegrationSelfW* // w1-w4 w6-w21
EUIntegrationLabW* // w1-w4 w6-w21
immigEconW* // w1-w4 w7-w8 w10-w11 w13-w17 w20-w21
immigCulturalW* // w1-w4 w7-w8 w10-w11 w13-w17 w20-w21 
partyIdStrengthW* // w1-w4 w6-w21
pidWeTheyW* // w1 w3 w4 w7 w9-w11 w13-w17 w20-w21
pidInterestedOthersW* // w1 w3 w4 w7 w9-w11 w13-w17 w20-w21
pidCriticisePartyW* // w1 w3 w4 w7 w9-w11 w13-w17 w20-w21
pidCommonPartyW //  w1 w3 w4 w7 w9-w11 w13-w17 w20-w21
partyIdStrengthW // w1-w4 w6-w21
generalElectionVoteW // all waves - if post election it is recall. 
likeLabW // alll waves
likeConW // all waves
partyIdSqueezeW // w1-w4 w6-w21

*/

keep wave* id partyIdW* EUIntegrationLabW* EUIntegrationConW* redistLabW* redistConW* redistSelfW* age* p_edlevelW* gender leftRightW* EUIntegrationSelfW*  immigEconW* immigCulturalW* lrLabW* euRefVoteW* generalElectionVoteW* force*  partyIdStrengthW* generalElectionVoteW* likeLabW* immigSelfW* likeConW* ageW* p_edlevelW* gender euRefVoteW* generalElectionVoteW* lrConW* partyIdSqueezeW* lrLDW* likeCorbynW* lrCorbynW* likeCameronW* labUnitedW* conUnitedW* labUnitedW* polAttentionW*


rename ageW* age*
rename p_edlevelW* education*
rename gender sex
rename euRefVoteW* referendumvote*
rename generalElectionVoteW* generalElectionVoteW*
rename polAttentionW* attention*


************************************
*		POLITICAL ATTENTION		   *
************************************

forval i=1/4 {
replace attention`i' = . if attention`i' > 10
}

forval i=6/8 {
replace attention`i' = . if attention`i' > 10
}

forval i=10/11 {
replace attention`i' = . if attention`i' > 10
}

forval i=13/17 {
replace attention`i' = . if attention`i' > 10
}

forval i=19/23 {
replace attention`i' = . if attention`i' > 10
}


************************************
**** 		REFERENDUM VOTE 	****
************************************


* 0 stay, 1 leave, 9999 dk, 2 wont vote
forval i=1/23 {
	gen refvote`i' = .
}

forval i=1/4 {
replace refvote`i' = 0 if referendumvote`i' == 0 
replace refvote`i' = 1 if referendumvote`i' == 1
}

forval i=6/19 {
replace refvote`i' = 0 if referendumvote`i' == 0 
replace refvote`i' = 1 if referendumvote`i' == 1
}

forval i=21/23 {
replace refvote`i' = 0 if referendumvote`i' == 0 
replace refvote`i' = 1 if referendumvote`i' == 1
}

 
 
 forval i=1/23 {
	gen refvotecat`i' = .
}


forval i=1/4 {
replace refvotecat`i' = 0 if referendumvote`i' == 0 // remain 
replace refvotecat`i' = 1 if referendumvote`i' == 1 // leave 
replace refvotecat`i' = 2 if referendumvote`i' == 9999 // dk
}

forval i=6/19 {
replace refvotecat`i' = 0 if referendumvote`i' == 0 // remain 
replace refvotecat`i' = 1 if referendumvote`i' == 1 // leave 
replace refvotecat`i' = 2 if referendumvote`i' == 9999 // dk
}

forval i=21/23 {
replace refvotecat`i' = 0 if referendumvote`i' == 0 // remain 
replace refvotecat`i' = 1 if referendumvote`i' == 1 // leave 
replace refvotecat`i' = 2 if referendumvote`i' == 9999 // dk
}



************************************
*    How United Parties are?	  **
************************************

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 {
	replace labUnitedW`i' = . if labUnitedW`i' == 9999
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 {
	replace conUnitedW`i' = . if conUnitedW`i' == 9999
}

************************************
*** 	corbyn like dislike    	 ***
************************************


forval i=7/19 {
	gen Corbynlike`i' = likeCorbynW`i'
}

forval i=7/19 {
replace Corbynlike`i' = . if likeCorbynW`i' > 10 
}


************************************
*** Corbyn's left-right position ***
************************************

	gen LR_Corbynw7 = lrCorbynW7
	replace LR_Corbynw7 = . if LR_Corbynw7>10
	
	gen LR_Corbynw8 = lrCorbynW8
	replace LR_Corbynw8 = . if LR_Corbynw8>10

	gen LR_Corbynw12 = lrCorbynW12
	replace LR_Corbynw12 = . if LR_Corbynw12>10

	
	
	
************************************
*** 	PARTY LIKE DISLIKE		 ***
************************************


forval i=1/23 {
	gen Labourlike`i' = likeLabW`i'
}

forval i=1/23 {
replace Labourlike`i' = . if likeLabW`i' > 10 
}

forval i=1/23 {
	gen Conlike`i' = likeConW`i'
}

forval i=1/23 {
replace Conlike`i' = . if likeConW`i' > 10 
}



***************************************************
*** 	Perception of Labour Party Position		***
***************************************************

forval i=2/12 {
replace lrLabW`i' = . if lrLabW`i' > 10
}

forval i=15/18 {
replace lrLabW`i' = . if lrLabW`i' > 10
}

forval i=20/23 {
replace lrLabW`i' = . if lrLabW`i' > 10
}



************************************************************
*** 	Perception of Conservative  Party Position		****
************************************************************

forval i=2/12 {
replace lrConW`i' = . if lrConW`i' > 10
}

forval i=15/18 {
replace lrConW`i' = . if lrConW`i' > 10
}

forval i=20/23 {
replace lrConW`i' = . if lrConW`i' > 10
}



*****************************************
***    perceived shift LABOUR PARTY   ***
*****************************************

gen perceivedshiftw6w7_L = lrLabW7 - lrLabW6
gen perceivedshiftw6w12_L = lrLabW12 - lrLabW6
gen perceivedshiftw6w18_L = lrLabW18 - lrLabW6


*****************************************
*** 	left-right self-placement	  ***
*****************************************


forval i=1/23 {
replace leftRightW`i' = . if leftRightW`i' > 10
}



**************************************************
***  respondent's redistribution preferences   ***
**************************************************

forval i=1/4 {
replace redistSelfW`i' = . if redistSelfW`i' > 10
}

forval i=6/7 {
replace redistSelfW`i' = . if redistSelfW`i' > 10
}

forval i=10/23 {
replace redistSelfW`i' = . if redistSelfW`i' > 10
}



**********************************************************
**   respondent's immigration (economic) preferences    ** 
**********************************************************

foreach i in 1 2 3 4 7 8 10 11 13 14 15 16 17 20 22 23 {
	replace immigEconW`i' = . if immigEconW`i'>7
}

foreach i in 1 2 3 4 7 8 10 11 13 14 15 16 17 20 22 23 {
	gen immigeconomic`i' = .
}


foreach i in 1 2 3 4 7 8 10 11 13 14 15 16 17 20 22 23 {
	replace immigeconomic`i' = 1 if immigEconW`i'==7
	replace immigeconomic`i' = 2 if immigEconW`i'==6
	replace immigeconomic`i' = 3 if immigEconW`i'==5
	replace immigeconomic`i' = 4 if immigEconW`i'==4
	replace immigeconomic`i' = 5 if immigEconW`i'==3
	replace immigeconomic`i' = 6 if immigEconW`i'==2
	replace immigeconomic`i' = 7 if immigEconW`i'==1
}

drop immigEconW* 




**********************************************************
**   respondent's immigration (cultural) preferences    ** 
**********************************************************


foreach i in 1 2 3 4 7 8 10 11 13 14 15 16 17 20 {
	replace immigCulturalW`i' = . if immigCulturalW`i'>7
}

foreach i in 1 2 3 4 7 8 10 11 13 14 15 16 17 20 {
	gen immigcultural`i' = .
}


foreach i in 1 2 3 4 7 8 10 11 13 14 15 16 17 20 {
	replace immigcultural`i' = 1 if immigCulturalW`i'==7
	replace immigcultural`i' = 2 if immigCulturalW`i'==6
	replace immigcultural`i' = 3 if immigCulturalW`i'==5
	replace immigcultural`i' = 4 if immigCulturalW`i'==4
	replace immigcultural`i' = 5 if immigCulturalW`i'==3
	replace immigcultural`i' = 6 if immigCulturalW`i'==2
	replace immigcultural`i' = 7 if immigCulturalW`i'==1
}

drop immigCulturalW* 


**********************************************************
**  	 respondent's EU Integration preferences 	    ** 
**********************************************************

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	replace EUIntegrationSelfW`i' = . if EUIntegrationSelfW`i'>10
}


******************************
***  PARTY IDENTIFICATION  ***
******************************

/*
1. Conservative; 
2. Labour; 
3. Liberal Democrat; 
4. Scottish National Party (SNP); 
5. Plaid Cymru; 
6. United Kingdom Independence Party; 
7. Green Party; 
8. British National Party (BNP); 
9. Other; 
10. No - none; 
9999. Don't know
11. Change UK- The Independent Group 
12. Brexit Party/Reform UK
*/

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen pid`i' = partyIdW`i'
}

forval i=1/4 {
replace pid`i' = 9 if pid`i' == 4 | pid`i' == 5 | pid`i' == 7 | pid`i' == 8 
}		// other party category

forval i=6/23 {
replace pid`i' = 9 if pid`i' == 4 | pid`i' == 5 | pid`i' == 7 | pid`i' == 8 
}		// other party category

forval i=16/23 {
replace pid`i' = 9 if pid`i' == 12  
}		// Brexit Party moved to other party category

replace pid16 = 9 if pid16==11 // Change UK - The independent group moved to other. 

forval i=1/4 {
replace pid`i' = 4 if pid`i' == 6
}		// ukip is 4 now

forval i=6/23 {
replace pid`i' = 4 if pid`i' == 6
}		// ukip is 4 now

forval i=1/4 {
replace pid`i' = 5 if pid`i' == 9
}	//other party category is now 5

forval i=6/23 {
replace pid`i' = 5 if pid`i' == 9
}	//other party category is now 5

forval i=1/4 {
replace pid`i' = 0 if pid`i' == 10 | pid`i' == 9999
}		// no pid and dk are 0

forval i=6/23 {
replace pid`i' = 0 if pid`i' == 10 | pid`i' == 9999
}		// no pid and dk are 0



label define parties 0"independent" 1"Conservative Party" 2"Labour Party" 3"Liberal Democrats" 4"UKIP"  5"other"

forval i=1/4 {
label values pid`i' parties
}

forval i=6/23 {
label values pid`i' parties
}


**************************************************
***  PARTY IDENTIFICATION (including leaners)  ***
**************************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen pidd`i' = pid`i'
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace pidd`i' = 1 if partyIdSqueezeW`i'==1 
}	// conservative leaners included now

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace pidd`i' = 2 if partyIdSqueezeW`i'==2
}	// labour leaners included now


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace pidd`i' = 3 if partyIdSqueezeW`i'==3
}	// libdem leaners included now

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace pidd`i' = 4 if partyIdSqueezeW`i'==6
}	// ukip leaners included now

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace pidd`i' = 5 if partyIdSqueezeW`i'==4 | partyIdSqueezeW`i'==5 | partyIdSqueezeW`i'==7 | partyIdSqueezeW`i'==8 | partyIdSqueezeW`i'==9
}		// other party category


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace pidd`i' = 0 if pid`i' == 10 & partyIdSqueezeW`i' == 10
replace pidd`i' = 0 if pid`i' == 9999 & partyIdSqueezeW`i' == 9999
}		// those saying no to both questions are 0


forval i=16/23 {
replace pidd`i' = 5 if pidd`i' == 12  
}		// Brexit Party moved to other party category


replace pidd16 = 5 if pidd16==11 // Change UK - The independent group moved to other. 


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
label values pidd`i' parties
}



***********************************************
*** 	LABOUR PID (excluding leaners)		***
***********************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen Labourpid`i' = pid`i'
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace Labourpid`i' = 1 if pid`i' == 2
replace Labourpid`i' = 0 if pid`i'!=2 & pid`i'!=. 
}	

label define Labourpid 0"non-Labour" 1"labour partisan"
label values Labourpid4 Labourpid 




***********************************************
*** 	LABOUR PID (including leaners)		***
***********************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen Labourpidd`i' = .
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace Labourpidd`i' = 1 if pidd`i' == 2
replace Labourpidd`i' = 0 if pidd`i'!=2 & pidd`i'!=. 
}	

label values Labourpidd4 Labourpid 



********************************************************
**** 	LABOUR VS INDEPENDENTS (excluding leaners)	****
********************************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen Labourind`i' = .
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace Labourind`i' = 1 if pid`i' == 2
replace Labourind`i' = 0 if pid`i'==0 
}	

label define labourind 0"independent" 1"labour partisan"
label values Labourind4 labourind 




********************************************************
****  Labour vs independents (including leaners)	****
********************************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen Labourindd`i' = .
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
replace Labourindd`i' = 1 if pidd`i' == 2
replace Labourindd`i' = 0 if pidd`i'==0 
}	

label values Labourind4 labourindd



*************************************
*** 		PID strength 		  ***	
*************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen strength`i' = .
}


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	replace strength`i' = 0 if pid`i'==0 // independent 
	replace strength`i' = 1 if partyIdStrengthW`i' == 3 & pid`i'==1 | pid`i'==2 | pid`i'==3 | pid`i'==4 | pid`i'==5 // not very strong 
	replace strength`i' = 2 if partyIdStrengthW`i'==2 & pid`i'==1 | pid`i'==2 | pid`i'==3 | pid`i'==4 | pid`i'==5 // fairly strong  
	replace strength`i' = 3 if partyIdStrengthW`i'==1 & pid`i'==1 | pid`i'==2 | pid`i'==3 | pid`i'==4 | pid`i'==5 // very strong
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {

	replace strength`i' = 1 if partyIdStrengthW`i' == 3 & pid`i'==1  // not very strong 
	replace strength`i' = 1 if partyIdStrengthW`i' == 3 & pid`i'==2  // not very strong 
	replace strength`i' = 1 if partyIdStrengthW`i' == 3 & pid`i'==3  // not very strong 
	replace strength`i' = 1 if partyIdStrengthW`i' == 3 & pid`i'==4  // not very strong 
	replace strength`i' = 1 if partyIdStrengthW`i' == 3 & pid`i'==5  // not very strong 

	replace strength`i' = 2 if partyIdStrengthW`i' == 2 & pid`i'==1  // fairly strong 
	replace strength`i' = 2 if partyIdStrengthW`i' == 2 & pid`i'==2  // fairly strong 
	replace strength`i' = 2 if partyIdStrengthW`i' == 2 & pid`i'==3  // fairly strong 
	replace strength`i' = 2 if partyIdStrengthW`i' == 2 & pid`i'==4  // fairly strong 
	replace strength`i' = 2 if partyIdStrengthW`i' == 2 & pid`i'==5  // fairly strong 

	replace strength`i' = 3 if partyIdStrengthW`i' == 1 & pid`i'==1  // very strong 
	replace strength`i' = 3 if partyIdStrengthW`i' == 1 & pid`i'==2  // very strong 
	replace strength`i' = 3 if partyIdStrengthW`i' == 1 & pid`i'==3  // very strong 
	replace strength`i' = 3 if partyIdStrengthW`i' == 1 & pid`i'==4  // very strong 
	replace strength`i' = 3 if partyIdStrengthW`i' == 1 & pid`i'==5  // very strong 
}	


***********************************************************
*** 	labour strength (labour versus independents) 1	***
***********************************************************


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen Labourind_strength`i' = .
}

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	replace Labourind_strength`i' = 0 if Labourind`i'==0 // independent
	replace Labourind_strength`i' = 1 if partyIdStrengthW`i' == 3 & Labourind`i'==1 // not very strong
	replace Labourind_strength`i' = 1 if partyIdStrengthW`i'==2 & Labourind`i'==1 // fairly strong
	replace Labourind_strength`i' = 2 if partyIdStrengthW`i'==1 & Labourind`i'==1 // very strong
}

label define categories 0"independent" 1"weak partisan" 2"strong partisan" 

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
label values Labourind_strength`i' categories  
}


***********************************************************
*** 	labour strength (labour versus independents) 2	***
***********************************************************

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen Labour_strength`i' = .
}


foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	replace Labour_strength`i' = 0 if Labourpid`i'==0 // independent
	replace Labour_strength`i' = 1 if partyIdStrengthW`i' == 3 & Labourpid`i'==1 // not very strong
	replace Labour_strength`i' = 2 if partyIdStrengthW`i'==2 & Labourpid`i'==1 // fairly strong
	replace Labour_strength`i' = 3 if partyIdStrengthW`i'==1 & Labourpid`i'==1 // very strong
}

label define categories2 0"non-Labour" 1"weak partisan" 2"moderate partisan" 3"strong partisan" 

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
label values Labour_strength`i' categories2 
}


******************************************************
** 		DV: SHIFT IN LEFT RIGHT SELF-PLACEMENT 		**
******************************************************


gen shiftw6w7_LR = leftRightW7 - leftRightW6



*******************************************************
***		create new pid variables as needed			***
*******************************************************

	*partisanship in wave 6 
	gen partyid6= . 							
	replace partyid6 = 1 if pid6==2	// labour pid
	replace partyid6 = 2 if pid6==1 // consevative pid
	replace partyid6 = 0 if pid6==0	// independents
	replace partyid6 = 3 if pid6==3 | pid6==4 | pid6==5 // other pid 
	
	label define labels_partyid6 0"independent" 1"Labour identification" 2"Conservative identification" 3"Other identification" 
	label values partyid6 labels_partyid6
	
	
	*Labour versus rest in wave 6 
	gen Labouridentification6 = .
	replace Labouridentification6 = 1 if pid6==2 // labour
	replace Labouridentification6 = 0 if pid6==0 | pid6==1 | pid6==3 | pid6==4 | pid6==5
	
	label define labels_Labouridentification6 0"the rest" 1"Labour identification"
	label values Labouridentification6 labels_Labouridentification6
	
	*Labour versus independents in wave 6
	gen Labouridentification6_ = .
	replace Labouridentification6_ = 1 if pid6==2 // labour
	replace Labouridentification6_ = 0 if pid6==0 
	
	label define labels_Labouridentification6_ 0"independent" 1"Labour identification"
	label values Labouridentification6_ labels_Labouridentification6_
	

**********************************************************
**		creating variables to limit the analyses  		**
**********************************************************

gen sample67 = 1 if wave6==1 & wave7==1


*********************************************************
****	centrist-leftist-rightist (based on LR)		 ****
*********************************************************

forval i=1/21 {
gen leftright`i' =.
}	

forval i=1/21 {
	replace leftright`i' = 0 if leftRightW`i'<5 & leftRightW`i'>=0
	replace leftright`i' = 1 if leftRightW`i'==5 
	replace leftright`i' = 2 if leftRightW`i'>5 & leftRightW`i'<11
}	

	

	label define labels_leftright 1"centrist" 0"left-wing" 2"right-wing"  
	label values leftright6 labels_leftright

	
egen waves_taken112 = rowtotal(wave1 wave2 wave3 wave4 wave5 wave6 wave7 wave8 wave9 wave10 wave11 wave12) // all waves takers from 1 to 12.
egen waves_taken120 = rowtotal(wave1 wave2 wave3 wave4 wave5 wave6 wave7 wave8 wave9 wave10 wave11 wave12 wave13 wave14 wave15 wave16 wave17 wave18 wave19 wave20) // all wave takers from 1 to 20



cd "~/Dropbox/EJPR/masspolarization/data"
save "data1.dta", replace



